﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class RadiatorTag : HYExternalCommand
	{
		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		public override Result HYExecute(ref string TranName)
		{
			TranName = "散热器标注";
			if (RadiatorTag.hWndRevit == null)
			{
				RadiatorTag.hWndRevit = new YJKWindowHandle(Process.GetCurrentProcess().MainWindowHandle);
			}
			RadiatorNumForm radiatorNumForm = new RadiatorNumForm(false);
			radiatorNumForm.Show(RadiatorTag.hWndRevit);
			int wMsg = 7;
			RadiatorTag.SendMessage(RadiatorTag.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			ISelectionFilter selectionFilter = new RadiatorSelectionFilter();
			Result result;
			for (;;)
			{
				try
				{
					IList<Element> list = base.Sel.PickElementsByRectangle(selectionFilter, "请框选需要修改的散热器...");
					if (radiatorNumForm.RadiatorNum == 0 && !radiatorNumForm.chkReadRadiatorNum.Checked)
					{
						YJKMessageBox.Show("散热器篇数不能为0！");
						continue;
					}
					int num = 0;
					foreach (Element element in list)
					{
						FamilyInstance familyInstance = element as FamilyInstance;
						if (!radiatorNumForm.chkReadRadiatorNum.Checked)
						{
							if (this.JudgeConnected(familyInstance))
							{
								num++;
								continue;
							}
							Parameter parameter = familyInstance.GetParameter("散热器片数");
							if (parameter != null && !parameter.IsReadOnly && !radiatorNumForm.chkReadRadiatorNum.Checked)
							{
								parameter.Set(radiatorNumForm.RadiatorNum);
							}
							base.Doc.Regenerate();
						}
						if (radiatorNumForm.IsDimension)
						{
							new RadiatorDimension().SetRadiatorDimension(familyInstance, base.Doc, radiatorNumForm.IsUp);
						}
					}
					radiatorNumForm.WriteData();
					if (num > 0)
					{
						YJKMessageBox.Show("您框选已连接的散热器不支持赋值功能！");
					}
					continue;
				}
				catch (Autodesk.Revit.Exceptions.OperationCanceledException)
				{
					radiatorNumForm.Close();
					result = Autodesk.Revit.UI.Result.Succeeded;
				}
				catch (Exception ex)
				{
					YJKMessageBox.Show(ex.Message);
					continue;
				}
				break;
			}
			return result;
		}

		private bool JudgeConnected(FamilyInstance fi)
		{
			bool result = false;
			ConnectorSetIterator connectorSetIterator = fi.MEPModel.ConnectorManager.Connectors.ForwardIterator();
			while (connectorSetIterator.MoveNext())
			{
				if ((connectorSetIterator.Current as Connector).IsConnected)
				{
					result = true;
					break;
				}
			}
			return result;
		}

		public static YJKWindowHandle hWndRevit;
	}
}
